/* 
 * shamefully copy from AnFetion.
 * more see: http://code.google.com/p/anfetion/
 */
package person.bangbang.im.Androidgin.Fetion;

import java.io.Serializable;

public class FetionFriend implements Serializable, Comparable<FetionFriend> {
    private static final long serialVersionUID = -4889702395437832128L;

    private static final String BR = "\n";

    private String lastSpeaker = null;
    boolean isMe = false;

    public String Uri;
    private String FriendName;
    private String NickName;
    public String mobile_no;
    public String impresa;
    public long portrait;
    public int score = 0;
    public int type = 0;
    public int onlineNotify = 0;
    public int group = 0;

    public int notifyID;
    public String lastMessage;
    public boolean IsMobileNo;

    private int fetionID = 0;

    private StringBuilder messageContent;
    private int onlineStatus;
    private String customStatus = null;

    public FetionFriend() {
        messageContent = new StringBuilder();
    }

    public int getFetionID() {
        if (fetionID == 0) {
            FetionUri u = new FetionUri(Uri);
            fetionID = StrUtil.parseInt(u.getId(), 0);
        }
        return fetionID;
    }

    public String getFriendName() {
        return FriendName;
    }

    public void setFriendName(String friendName) {
        if (friendName != null && friendName.length() > 0) {
            FriendName = friendName;
        }
    }

    public String getNickName() {
        return NickName;
    }

    public void setNickName(String nickName) {
        if (nickName != null && nickName.length() > 0) {
            NickName = nickName;
        }
    }

    public int getOnlineStatusInt() {
        return onlineStatus;
    }

    public void setOnlineStatus(int onlineStatus) {
        setOnlineStatus(onlineStatus, null);
    }

    public void setOnlineStatus(int onlineStatus, String status) {
        this.onlineStatus = onlineStatus;
        this.customStatus = status;
    }

    public String getOndlineStatusStr() {
        if (customStatus != null) return "[" + customStatus + "]";
        return Settings.getStatusDesc(onlineStatus);
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();

        if (FriendName == null || FriendName.equals("")) sb.append(new FetionUri(this.Uri).id);
        else sb.append(FriendName);

        sb.append(" ").append(getOndlineStatusStr());

        if (impresa != null) sb.append(" " + impresa);

        return sb.toString();
    }

    public String toShortString() {
        StringBuffer sb = new StringBuffer();

        sb.append(getName());
        sb.append(" ").append(getOndlineStatusStr()).append(getType());
        return sb.toString();
    }

    public String addMessage(String name, String message) {
        if (messageContent.length() > 0) {
            messageContent.append(BR);
            if (!name.equals(lastSpeaker)) {
                messageContent.append(BR);
            }
        }
        StringBuilder ret = new StringBuilder().append(name).append(" 说(").append(StrUtil.getDate()).append(
                "):").append(BR).append(message);
        messageContent.append(ret);
        lastSpeaker = name;
        return ret.toString();
    }

    public StringBuilder getMessage() {
        return messageContent;
    }


    public int compareTo(FetionFriend another) {
        // System.out.println(this.FriendName + " vs " + another.FriendName);
        if (this.isMe) return -1;
        
        // BUGFIX: java.lang.NullPointerExeception
        // bangbang.song@gmail.com
        if (null == another) {
        	//TODO How can i do under this condition???
        	return -1;
        }
        if (another.isMe) return 1;
        if (this.notifyID != another.notifyID) {
            if (another.notifyID == 0) return -1;
            if (this.notifyID == 0) return 1;
        }
        int ret = 0 - (this.getOnlineStatusInt() - another.getOnlineStatusInt());
        if (ret != 0) {
            if (this.getOnlineStatusInt() == Settings.STATUS_ONLINE) return -1;
            if (another.getOnlineStatusInt() == Settings.STATUS_ONLINE) return 1;
            return ret;
        }

        String s1 = this.getName();
        if (s1 == null) return 1;
        String s2 = another.getName();
        if (s2 == null) return -1;
        return s1.compareTo(s2);

    }
    
    public String getType() {
        return type == Settings.TYPE_REFUSE ? "(拒绝)" : type == Settings.TYPE_NOT_FRIEND ? "(等待同意)" :""; 
    }

    public String toFullString() {
        return "name = " + FriendName + "; nick = " + NickName + "; uri = " + Uri + "; mobile = " + mobile_no
                + "; portrait = " + portrait + "; score = " + score + "; type = " + type
                + "; onlineNotify = " + onlineNotify + "; group = " + group + "; status = " + onlineStatus;
    }

    public String getName() {
        return FriendName != null ? FriendName : (NickName != null ? NickName : mobile_no != null ? mobile_no
                : new FetionUri(this.Uri).id);
    }

}
